﻿using FileToVideo.Class;
using FileToVideo.Enums;
using FileToVideo.Helpers;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace FileToVideo.Project
{
    /// <summary>
    /// 转换工具
    /// </summary>
    public class Convert
    {
        /// <summary>
        /// 初始化检查文件存在
        /// </summary>
        private bool _initCheckFile = true;

        /// <summary>
        /// 输入文件地址
        /// </summary>
        public string inputFilePath;

        /// <summary>
        /// 输出文件地址
        /// </summary>
        public string outputFilePath;

        /// <summary>
        /// 转换模式
        /// </summary>
        private ConvertMode convertMode;

        /// <summary>
        /// 加密模式
        /// </summary>
        public CodingMode codingMode;

        /// <summary>
        /// 编码模式
        /// </summary>
        public EncryptMode encryptMode;

        /// <summary>
        /// 密码
        /// </summary>
        public string password = "";

        /// <summary>
        /// 密码数据
        /// </summary>
        public string PasswordStr
        {
            get
            {
                string ret = "";
                try
                {
                    switch (encryptMode)
                    {
                        case EncryptMode.AES:
                            ret = Item.MD5(password);
                            break;
                        case EncryptMode.BASE64:
                            ret = Base64.GetKey(password, codingMode);
                            break;
                        default:
                            break;
                    }
                }
                catch (Exception err)
                {
                    MessageBox.Show(err.Message, "错误!", MessageBoxButton.OK, MessageBoxImage.Error);
                }
                return ret;
            }
        }

        /// <summary>
        /// 文件是否存在?
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static bool FileExist(string path)
        {
            return System.IO.File.Exists(path);
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="convertMode"></param>
        /// <param name="inputFilePath"></param>
        /// <param name="outputFilePath"></param>
        /// <param name="codingMode"></param>
        /// <param name="encryptMode"></param>
        /// <param name="password"></param>
        public Convert(ConvertMode convertMode, string inputFilePath, string outputFilePath, CodingMode codingMode, EncryptMode encryptMode, string password)
        {
            this.convertMode = convertMode;
            this.inputFilePath = inputFilePath;
            this.outputFilePath = outputFilePath;
            this.codingMode = codingMode;
            this.encryptMode = encryptMode;
            this.password = password;

            if (_initCheckFile)
            {
                CheckFileExist();
            }
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="convertMode"></param>
        public Convert(ConvertMode convertMode) : this(convertMode, "", "", CodingMode.NONE, EncryptMode.AES, "")
        {
            _initCheckFile = false;
        }

        /// <summary>
        /// 检测文件是否存在
        /// </summary>
        /// <exception cref="ConvertException"></exception>
        public bool CheckFileExist()
        {
            if (!FileExist(inputFilePath))
            {
                throw new ConvertException("输入文件不存在!");
            }
            if (FileExist(outputFilePath))
            {
                throw new ConvertException("输出文件已存在!");
            }
            return true;
        }

        /// <summary>
        /// 转换文件
        /// </summary>
        /// <returns></returns>
        public bool ConvertFile()
        {
            bool ret = false;
            switch (convertMode)
            {
                case ConvertMode.FILE2VIDEO:
                    ret = ConvertFile2Video();
                    break;
                case ConvertMode.VIDEO2FILE:
                    ret = ConvertVideo2File();
                    break;
                case ConvertMode.FILE2IMAGE:
                    break;
                case ConvertMode.IMAGE2FILE:
                    break;
                default:
                    break;
            }
            return ret;
        }

        /// <summary>
        /// 文件转视频
        /// </summary>
        /// <returns></returns>
        private bool ConvertFile2Video()
        {
            bool ret = false;

            if (CheckFileExist())
            {

            }

            return ret;
        }

        /// <summary>
        /// 视频转文件
        /// </summary>
        /// <returns></returns>
        private bool ConvertVideo2File()
        {
            bool ret = false;

            return ret;
        }
    }
}
